import BasePageCtrl, { AnimType } from "./BasePageCtrl";
import UiManager from "./UiManage";
import n from "./AudioManager"
import l from "./PlayerDataSys"
import s from "./AbTestMgr"
import c from "./PeripheralSystemDataMgr"
import u from "./EventMgr"
import p from "./GameEventType"
import d from "./EngineUtil"
import h from "./MathUtil"
import f from "./BasePageCtrl"
import g from "./PageMgr"
import y from "./UiManage"
import m from "./ArriveCityRewardPage"
import MathUtils from "./MathUtil";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ArriveCityRewardPageCtrl extends BasePageCtrl {
    ui = null;

    onLoad() {
        this.onUILoad();
        this._animType = AnimType.SCALE;
        this._touchControl = false;
        this._hasPeneLock = true;
        this._hasBlack = true;
        this._hasTouchLock = false;
        this._hasBlackTouch = false;
        super.onLoad();
        this.addButtonListen();
    }

    start() { }

    onUILoad() {
        this.ui = this.node.addComponent(m);
    }

    _init() {
        this.initUI();
    }

    initUI() {
        var e = c.getTravleData(),
            t = s.isOptimizeTravelFundFlag(),
            a = e.cash,
            o = e.money,
            i = e.travel_cash_info;

        if (t) {
            this.ui.cashNode.active = false;
            this.ui.fundNode.active = false;
            this.ui.cashExperimentNode.active = true;
            this.ui.cashExperimentLabel.getComponent(cc.Label).string = d.getCashBalance(a) + "元";
        } else {
            this.ui.cashNode.active = true;
            this.ui.fundNode.active = true;
            this.ui.cashExperimentNode.active = false;
            this.ui.cashLabel.getComponent(cc.Label).string = d.getCashBalance(a) + "元";
            this.ui.fundLabel.getComponent(cc.Label).string = d.getCashBalance(o) + "元";
        }

        var r = i.need,
            l = MathUtils.accMul(i.percent, 100),
            u = "<b><color=#FFFFFF>再抵达 </color><color=#FF5442>" + r + " </color><color=#FFFFFF>个城市</color></b>",
            p = "<b><color=#FFFFFF>红包提现比例即可升至 </color><color=#FF5442>" + (l = this.fomatFloat(l, 2)) + "%</color></b>";

        n.getInstance().playMusic("cityReward");

        this.ui.cashNode.opacity = 0;
        this.ui.fundNode.opacity = 0;
        this.ui.light1.active = false;
        this.ui.light2.active = false;
        this.ui.proTips1.getComponent(cc.RichText).string = "";
        this.ui.proTips2.getComponent(cc.RichText).string = "";
        this.ui.btn_accept.opacity = 0;
        this.ui.btn_accept.getComponent(cc.Button).interactable = false;

        this.playFadeInAction(u, p);
    }

    playFadeInAction(e, t) {
        var a = this;

        var o = function () {
            n.getInstance().playMusic("typing", true);
            d.typingAni(a.ui.proTips1.getComponent(cc.RichText), e, 100, function () {
                i();
            });
        }

        var i = function () {
            d.typingAni(a.ui.proTips2.getComponent(cc.RichText), t, 100, function () {
                n.getInstance().stopMusic("typing", false);
                r();
            });
        }

        var r = function () {
            cc.tween(a.ui.btn_accept).to(1, {
                opacity: 255
            }).call(function () {
                a.ui.btn_accept.getComponent(cc.Button).interactable = true;
            }).start();
        }

        cc.tween(this.ui.cashNode).to(1, {
            opacity: 255
        }).call(function () {
            a.ui.light1.active = true;
        }).start();

        cc.tween(this.ui.fundNode).to(1, {
            opacity: 255
        }).call(function () {
            a.ui.light2.active = true;
            o();
        }).start();
    }

    addButtonListen() {
        UiManager.addButtonListen(this.ui.btn_accept, this.clickAccept, this);
    }

    clickAccept() {
        var e = this;
        this.clickClose();
        var t = c.getTravleData(),
            a = t.cash,
            o = t.money,
            i = t.cash_before,
            r = t.cash_after,
            n = a > 0,
            d = o > 0;

        if (n || d) {
            if (n) {
                var h = s.isOptimizeTravelFundFlag() ? this.ui.redPaperExperiment.getPosition() : this.ui.redPaper.getPosition();
                u.trigger(p.SHOWEFFECT_FLYINGRED, {
                    startPos: h,
                    type: 0,
                    callback: function () {
                        u.trigger(p.INIT_TOP_PRO_SHOW);
                        l.updateCashBalace(a);
                        i == r ? e.scheduleOnce(function () {
                            c.checkShowGameStartPage();
                        }, 1) : g.showPage("WithDrawProportionIncreasePage");
                    }
                });
            }

            if (d) {
                u.trigger(p.SHOWEFFECT_FLYINGRED, {
                    startPos: this.ui.fund.getPosition(),
                    type: 1,
                    callback: function () {
                        u.trigger(p.INIT_TOP_PRO_SHOW);
                        if (!n) {
                            if (i == r) {
                                e.scheduleOnce(function () {
                                    c.checkShowGameStartPage();
                                }, 1);
                            } else {
                                g.showPage("WithDrawProportionIncreasePage");
                            }
                        }
                        l.updateGoldBalace(o);
                    }
                });
            }
        } else {
            u.trigger(p.INIT_TOP_PRO_SHOW);
            if (i == r) {
                c.checkShowGameStartPage();
            } else {
                g.showPage("WithDrawProportionIncreasePage");
            }
        }
    }

    clickClose() {
        this.hide();
    }

    fomatFloat(e, t) {
        return Math.round(e * Math.pow(10, t)) / Math.pow(10, t);
    }

    static prefabUrl = "ArriveCityRewardPage";
    static className = "ArriveCityRewardPageCtrl";
}
